package com.yjc.test;

import com.yjc.bean.Department;
import com.yjc.bean.Employee;
import com.yjc.bean.Post;
import com.yjc.mapper.DepartmentMapper;
import com.yjc.mapper.EmployeeMapper;
import com.yjc.mapper.PostMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

public class MyTest01 {
    SqlSession sqlSession = null;

    @Before
    public void init() throws IOException {
        String path = "mybatis-config.xml";//路径
        InputStream inputStream = Resources.getResourceAsStream(path);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = sessionFactory.openSession();
    }
    //    1.查询指定id的员工（5）
    @Test
    public void test01(){
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
        System.out.println(employeeMapper.selectEmpById(1));
    }

//    2.添加新员工（10）
    @Test
    public void test02(){
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
        employeeMapper.addEmployee(new Employee("xxx",3,new Date(),10000.0,8,4));
        sqlSession.commit();
        sqlSession.close();
    }

//    3.根据id 删除员工（10）
    @Test
    public void test03(){
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
        employeeMapper.deleteEmpById(1);
    }
//    4.查询所有员工，并分页。要求查询第2页的员工，一页5条（10）
    @Test
    public void test04(){
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
        List<Employee> employeeList= employeeMapper.selectEmpAll(2,5);
        for (Employee employee : employeeList) {
            System.out.println(employee);
        }
    }
//    5、根据部门名字查询部门内员工的信息（只需要查出员工信息即可）（10）
    @Test
    public void test05(){
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
        List<Employee> employeeList= employeeMapper.selectEmpBydName("财务部");
        for (Employee employee : employeeList) {
            System.out.println(employee);
        }
    }
//    6、查询出所有部门信息，并包含这个部门的员工信息（使用 collection 标签完成）（10）
    @Test
    public void test06(){
        DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class);
        List<Department> departmentList=departmentMapper.selectAllDepatment();
        for (Department department : departmentList) {
            System.out.println("部门------");
            System.out.println(department);
            System.out.println("员工");
            for (Employee employee : department.getEmployeeList()) {
                System.out.println(employee);
            }
        }
    }
//    7、查询所有员工信息，并包含其直属上级员工信息（使用 association 标签完成）（10）
    @Test
    public void test07() {
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
        List<Employee> employeeList= employeeMapper.selectEmpAndBoss();
        for (Employee employee : employeeList) {
            System.out.println("本人------");
            System.out.println(employee);
            System.out.println("上级");
            System.out.println(employee.getEmployee());
        }
    }
//    8、根据部门id查询部门信息和部门内的员工信息。（要求查出部门信息和部门员工信息）（10）
    @Test
    public void test08(){
        DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class);
        List<Department> departmentList=departmentMapper.selectAllDepatmentById(1);
        for (Department department : departmentList) {
            System.out.println("部门------");
            System.out.println(department);
            System.out.println("员工");
            for (Employee employee : department.getEmployeeList()) {
                System.out.println(employee);
            }
        }
}
//    9、根据职务名称，查询职务信息，并包含处于此职务的员工信息（包括所属部门名称）（职务到员工之间属于1对多的关系，员工到部门之间属于1对1关系）（20）
    @Test
    public void test09(){
        PostMapper postMapper = sqlSession.getMapper(PostMapper.class);
        List<Post> postList = postMapper.selectPostAndEmpDepartBypName("部门经理");
        for (Post post : postList) {
            System.out.println("职务----------");
            System.out.println(post);
            System.out.println("员工-----");
            for (Employee employee : post.getEmployeeList()) {
                System.out.println(employee);
                System.out.println("部门---");
                System.out.println(employee.getDepartment());
            }
        }
    }
}
